package cn.zhc.com.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.TextView;

import cn.zhc.com.R;


/**
 * Created by zhouhongchun on 2017/9/6.
 * 通用的dialog ，使用builder模式创建
 * 注意：如果设置不使用默认布局，且新的布局和默认布局大致相同才可以使用，使用是请按照默认布局命名控件
 */
public class CustomAlertDialog extends Dialog {
    private CustomAlertDialog(Context context, int theme) {
        super(context, theme);
    }

    public interface OnCustomDialogListener {
        void onCallBack(boolean isPositive);
    }

    public static class Builder {

        private CustomAlertDialog.OnCustomDialogListener mOnCustomDialogListener;
        private Context mContext;
        private TextView tvTitle ;
        private TextView tvMessage ;
        private TextView tvPositive ;
        private TextView tvNegative ;
        /**
         * 标题
         */
        private String mTitle = "标题";
        /**
         * 内容
         */
        private String mMessage = "内容";
        /**
         * 确认按钮内容
         */
        private String mPositiveText = "确定";
        /**
         * 取消按钮内容
         */
        private String mNegativeText = "取消";
        /**
         * 默认的布局
         */
        private int mDefaultLayoutID = R.layout.dialog_layout;
        /**
         * viewRoot 根布局
         */
        private View mViewRoot;

        /**
         * 获取根布局索引
         *
         * @return
         */
        public View getViewRoot() {
            return mViewRoot;
        }

        public Builder(Context context) {
            this.mContext = context;
        }

        /**
         * @param message 内容
         * @return
         */
        public CustomAlertDialog.Builder addMessage(String message) {
            this.mMessage = message;
            return this;
        }

        /**
         * @param title 标题
         * @return
         */
        public CustomAlertDialog.Builder addTitle(String title) {
            this.mTitle = title;
            return this;
        }

        /**
         * @param mNegativeText 取消按钮内容
         * @return
         */
        public CustomAlertDialog.Builder addNegativeText(String mNegativeText) {
            this.mNegativeText = mNegativeText;
            return this;
        }

        /**
         * @param mPositiveText 确认按钮内容
         * @return
         */
        public CustomAlertDialog.Builder addPositiveText(String mPositiveText) {
            this.mPositiveText = mPositiveText;
            return this;
        }


        /**
         * 使用自定义的布局，请注意控件的命名必须参照默认布局
         */
        public CustomAlertDialog.Builder addView(View view) {
            this.mViewRoot = view;
            return this;
        }

        /**
         * 添加监听
         *
         * @return
         */
        public CustomAlertDialog.Builder addOnCustomDialogListener(CustomAlertDialog.OnCustomDialogListener mOnCustomDialogListener) {
            this.mOnCustomDialogListener = mOnCustomDialogListener;
            return this;
        }

        /**
         * 自定义布局文件时必须要按照下面来命名控件
         * @param view
         */
        private void initView(View view){
            tvTitle = (TextView) view.findViewById(R.id.title);
            tvMessage = (TextView) view.findViewById(R.id.message);
            tvNegative = (TextView) view.findViewById(R.id.negative);
            tvPositive = (TextView) view.findViewById(R.id.positive);

            tvTitle.setText(mTitle);
            tvMessage.setText(mMessage);
            tvNegative.setText(mNegativeText);
            tvPositive.setText(mPositiveText);

        }

        /**
         * 设置点击事件
         */
        private void initEvent(){
            tvNegative.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnCustomDialogListener.onCallBack(false);
                }
            });
            tvPositive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnCustomDialogListener.onCallBack(true);
                }
            });
        }
        public CustomAlertDialog build() {
            final CustomAlertDialog mCustomDialog = new CustomAlertDialog(mContext, R.style.AlertDialog);
            if (mViewRoot == null) {
                this.mDefaultLayoutID = R.layout.dialog_layout;
                this.mViewRoot = View.inflate(mContext, mDefaultLayoutID, null);
            }
            //mCustomDialog.setOnKeyListener();
            //按返回键，取消dialog,默认是true，ProgressDialog默认为false
            mCustomDialog.setCancelable(true);
            //点击空白地方，不取消dialog,默认是true
            mCustomDialog.setCanceledOnTouchOutside(false);
            //设置弹窗动画
            mCustomDialog.getWindow().getAttributes().windowAnimations = R.style.CustomDialogAnimation;
            mCustomDialog.setContentView(mViewRoot);
            initView(mViewRoot);
            initEvent();

            mCustomDialog.setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {

                }
            });
            return mCustomDialog;
        }

    }
}
